import requests
from bs4 import BeautifulSoup



def parse(url):

    # 由于一般网站都是供用户访问 如果检测到User-Agent是黑客或者其他可能拒绝访问 故此处模拟浏览器
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    # 以防乱码 此处将其编码设置为utf-8 因为有中文
    response.encoding = 'utf-8'

    # print("开始解析网页=》=》=》")
    # print("页面地址：  "+url)

    bs = BeautifulSoup(response.text, "html.parser")
    name = bs.title.string
    # print("页面标题：  "+bs.title.string)

    downloads = bs.select(".download")
    if not downloads:
        return {}
    videoNodeList = downloads(".download")[0]#css选择器
    videoUrl = videoNodeList.find("a")["href"]
    suffix = videoUrl.rsplit('.', maxsplit=1)[1]

    resultJson = {"name":name,"url":videoUrl,"suffix":suffix}

    # print(resultJson)
    # print("解析结束=》=》=》")
    return resultJson;

# 测试
# from downloadFiles import download
# url="https://www.c3f1d.com/view/202112/39643.html"
# resultJson = parse(url)
# print(resultJson["videoUrl"])
# download(resultJson["videoUrl"],"G:/pythonDown/vio/")
